<?php
namespace app\api\controller;

use app\api\model\SortModel;

use think\Controller;
use think\Db;
use think\Request;
use think\Cache;
use think\Cookie;
use think\Session;
class Sort extends Common
{


    public function index()
    {
        $db=Db::name("sort");
        // 用户权限
        $role = $this->roles_users();
        // $list = model('Sort')-> getTree();
        $list = DB::name('sort')->field('id,catname,pid')->where('pid=0')->order('disorder asc,id asc')->select();
        // 一级菜单
        $arr = array();
        foreach ($list as $k => $v) {
            $arr[$k]['id'] = $v['id'];
            $arr[$k]['catname'] = $v['catname'];
            if(!empty($role[0]) && !in_array($v['id'],$role)){
                unset($arr[$k]);
            }
            // 二级菜单
            $list2 = $db->where(array('status'=>1,'pid'=>$v['id']))->order('disorder asc,id asc')->select();
            $arr2 = array();
            foreach ($list2 as $k2 => $v2) {
                $arr2[$k2]['id'] = $v2['id'];
                    $arr2[$k2]['catname'] = $v2['catname'];
                    if(!empty($role[0]) && !in_array($v2['id'],$role)){
                        unset($arr2[$k2]);
                    }
                    // 三级菜单
                    $arr3 = array();
                    $list3 = $db->where(array('status'=>1,'pid'=>$v2['id']))->order('disorder asc,id asc')->select();
                    foreach ($list3 as $k3 => $v3) {
                        $arr3[$k3]['id'] = $v3['id'];
                        $arr3[$k3]['catname'] = $v3['catname'];
                        // $arr3[$k3]['children'] = array();
                        if(!empty($role[0]) && !in_array($v3['id'],$role)){
                            unset($arr3[$k3]);
                        }
                    }
                if($arr3) $arr2[$k2]['children'] = $arr3;
            }
            if($arr2) $arr[$k]['children'] = $arr2;
        }
        $res['data']=$arr;
        $res['meta']=array('msg'=>'获取栏目列表成功','status'=>200);
        return json_encode($res);
    }

    public function check()
    {
        $db=Db::name("sort");
        
        $list = Db::name('sort')
        ->field('id,catname')
        ->where('pid != 0 and status = 1')
        ->select();
        $arr['data']=$list;
        foreach ($list as $k => $v) {
            $arr['arr'][] = $v['id'];
        }
        if($list){
            $arr['meta']=array('msg'=>'获取权限列表成功！','status'=>200);
        }else{
            $arr['meta']=array('msg'=>'获取权限列表失败！','status'=>300);
        }
        return json_encode($arr);

    }

    public function add()
    {
        $id=input('id',0);
        $admin_uid=$_SESSION['think']['admin_uid'];
        if(Request::instance()->isPost()){
            $data=$_POST;

            $model_arr=explode("|",input('request.model_id'));
            $data['model_id']=$model_arr[0];
           // print_r($data);
           // die;
            //$data['model_fields']=implode($_POST['model_fields'],",");
            if($id){
                if(Db::name("sort")->where("id=".$id)->update($data)){
                    $this->success("修改成功","/admin/sort/index.html");
                }else{
                    $this->error("修改失败");
                }
            }else{
                if(Db::name("sort")->insert($data)){
                    $this->success("添加成功","/admin/sort/index.html");
                }else{
                    $this->error("添加失败");
                }
            }

        }



        if($id){
            $v=Db::name("sort")->find($id);
        }else{
            $pid=input('pid',0);

            if($pid){
                $v=Db::name("sort")->find($pid);
                $v['catname']="";
                $v['encatname']="";
                $v['seotitle']="";
                $v['seokeywords']="";
                $v['seodescription']="";
                $v['pid']=$pid;
                $v['id']='';
                // print_r($v);die;
            }else{
                $v['id']="";
                $v['catname']="";
                $v['encatname']="";
                $v['seotitle']="";
                $v['seokeywords']="";
                $v['seodescription']="";
                $v['templet']="";
                $v['sort_c']="";
                $v['sort_a']="";
                $v['imgnum']=0;
                $v['imgsize']="";
                $v['imgname']="";
                $v['titleimgsize']="";
                $v['sort_url']="";
                $v['outlinkurl']="";
                $v['inlinkurl']="";
                $v['isopen']=0;
                $v['sort_p']="";
                $v['waterimg']="";
                $v['waterlocation']="";
                $v['waterimgtype']="";
                $v['img1']="";
                $v['img2']="";
                $v['status']=1;
                $v['model_id']=1;
                $v['pid']=$pid;
                $v['model_id']=0;
                $v['imgtype']=0;

            }
            $v['disorder']=Db::name("sort")->max('disorder')+5;
        }
//        print_r($v);
        // print_r(model('Sort')-> getTree());die;
        $this->assign("v",$v);
        $this->assign("admin_uid",$admin_uid);
        $this->assign("sort",model('Sort')-> getTree());
        $this->assign("sort2",$this->get_mytable_list('model'));
        return $this->fetch("/sort/add");
    }


    public function del()
    {
        $id=input('id',0);
        if(Db::name("sort")->delete($id)){
            $this->success("删除成功","/admin/sort/index.html");
        }else{
            $this->error("删除失败");
        }



    }

    public function audit()
    {

        $id=$_POST['id'];
//        echo $id;

        $v=Db::name("sort")->find($id);
        $data['status']=$v['status']?0:1;

        if(Db::name("sort")->where("id=".$id)->update($data)){
//            $this->success("审核成功","/admin/model/index.html");
            $return_data['status'] = 0;
            $return_data['msg'] ="审核成功";
        }else{
//            $this->error("审核失败");
            $return_data['status'] = 1;
            $return_data['msg'] ="审核失败";
        }

        echo json_encode($return_data);


    }




    //生成缓存
    public function Make(){
        //生成模型缓存
        $model=Db::name('model')->where("status=1")->select();
        foreach ((array)$model as $k => $v){
            Cache::set("model_{$v['id']}",$v);//存入缓存
        }

        //生成角色缓存
        $role=Db::name('role')->where("status=1")->select();
        foreach ((array)$role as $k => $v){
            Cache::set("role_{$v['id']}",$v);//存入缓存
        }

        //生成管理员缓存
        $manager=Db::name('manager')->where("status=1")->select();
        foreach ((array)$manager as $k => $v){
            Cache::set("manager_{$v['id']}",$v);//存入缓存
        }

        //生成栏目缓存
        $sort=Db::name('sort')->where("status=1")->select();
        foreach ((array)$sort as $k => $v){
            Cache::set("sort_{$v['id']}",$v);//存入缓存
        }

        //生成表单缓存
        $form=Db::name('forms')->where("status=1")->select();
        foreach ((array)$form as $k => $v){
            Cache::set("forms_{$v['id']}",$v);//存入缓存
        }

        //生成系统配置缓存
        $sort=Db::name('config')->select();
        foreach ((array)$sort as $k => $v){
            Cache::set("{$v['varname']}",$v['varvalue']);//存入缓存
        }

        $this->success("缓存更新成功",Cookie::get('Jumpurl'));
    }
}